package com.wwh.furns.filter;

import com.wwh.furns.utils.JDBCUtilsByDruid;

import javax.servlet.*;
import java.io.IOException;
import java.rmi.RemoteException;

/**
 * @author Albert
 * @date 2024/8/11-21:21
 * @describe 管理事务的过滤器
 */
public class TransactionFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        try {
            //放行
            filterChain.doFilter(servletRequest, servletResponse);
            JDBCUtilsByDruid.commit();
            System.out.println("进入管理事务的控制器");
        } catch (Exception e) {
            System.out.println("进行回滚");
            JDBCUtilsByDruid.rollback();
            throw new RuntimeException(e);
        }
    }

    @Override
    public void destroy() {

    }
}
